<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Withdraw extends CI_Controller
{

    private $shop_id;
    private $post;

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('form_validation'); //加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('common');
        $this->load->model('withdraw_model');

        $this->init();
        // $this->output->set_content_type('application/html;charset=utf-8');
    }

    //签名验证 @1
    public function init()
    {

      $post = $this->post = $this->input->post();
      $post['session_key']  = isset($post['session_key'])?trim($post['session_key']):'';
      if($post['session_key']&&$session_key = $post['session_key']){
        if($userInfo = $this->db->query("select * from app_user where session_key = '$session_key'")->row_array())
        {
          if($tel = $userInfo['tel'])
          {
            if($shopTel = $this->db->query("select * from app_shop_tel where tel = '$tel'")->row_array())
            {
              $this->shop_id = $shopTel['shop_id'];
            }else{
              send_json(array('code' => 4002, 'data' => '', 'msg' => "验签失败，请重新授权登录"));
            }
          }else{
            send_json(array('code' => 4002, 'data' => '', 'msg' => "验签失败，请重新授权登录"));
          }
        }else{
          send_json(array('code' => 4002, 'data' => '', 'msg' => "验签失败，请重新授权登录"));
        }
      }else{
        send_json(array('code' => 4001, 'data' => '', 'msg' => "参数错误"));
      }
    }

    //私有函数 获取可提现金额
    private function PrgetAccMoney($shop_id)
    {
      return $this->db->query("select * from app_property where shop_id = '$shop_id' ")->row()->normal;
    }

    /**
     * @SWG\Post(
     *     summary="获取可提现金额",
     *     path="/withdraw/getAccMoney",
     *     tags={"商户端"},
     *     operationId="getAccMoney",
     *     produces={"application/json"},
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     )
     *  )
     */
    public function getAccMoney()
    {
      $post = $this->post;
      $shop_id = $this->shop_id;
      $data = $this->db->query("select * from app_property where shop_id = '$shop_id' ")->row_array();
      if($data){
        send_json(array('code' => 2000, 'data' => $data));
      }else{
        send_json(array('code' => 4000, 'data' => $data,'msg'=>'未找到信息'));
      }
    }

    //提现 需要提现方式type，姓名name,提现账号account,提现金额money
    /**
     * @SWG\Post(
     *     summary="提现",
     *     path="/withdraw/tixian",
     *     tags={"商户端"},
     *     operationId="tixian",
     *     produces={"application/json"},
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     *     @SWG\Parameter(
     *         description="提现方式",
     *         in="formData",
     *         name="type",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="姓名",
     *         in="formData",
     *         name="name",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="提现账号",
     *         in="formData",
     *         name="account",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="提现金额",
     *         in="formData",
     *         name="money",
     *         required=true,
     *         type="integer",
     *     )
     * )
     */
    public function tixian()
    {
        $post = $this->post;
        $post['shop_id'] = $shop_id = $this->shop_id;
        if ($post['type'] && $post['name'] && $post['account']&&$post['money']) {
            $normalMoney = $this->PrgetAccMoney($shop_id);
            if ($normalMoney<$post['money']) {
                send_json(array('code' => 4000, 'data' => '', 'msg' => "提现金额大于可提现金额"));
            }
            $res = $this->withdraw_model->apply_for_tixian($post);
            if ($res) {
                send_json(array('code' => 2000, 'data' => "", 'msg' => "提现成功，等待处理"));
            } else {
                send_json(array('code' => 4000, 'data' => '', 'msg' => "提现失败"));
            }

        } else {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "参数不全"));
        }
    }

    //获取提现列表
    /**
     * @SWG\Post(
     *     summary="获取提现列表",
     *     path="/withdraw/withdrawList",
     *     tags={"商户端"},
     *     operationId="withdrawList",
     *     produces={"application/json"},
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     )
     * )
     */
    public function withdrawList()
    {
        $post = $this->post;
        $post['shop_id'] = $shop_id = $this->shop_id;
        $res = $this->withdraw_model->get_withdrawal_list($shop_id);
        if ($res) {
            send_json(array('code' => 2000, 'data' => $res, 'msg' => ""));
        }
    }
}
